<?php

namespace app\index\controller;

class PieceController extends noLoginBase
{
	public function index()
	{
		$this->assign("title", "计件工资");

		return $this->fetch();
	}

	/**
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * 获取用户信息
	 */
	public function getUserInfo()
	{
		$user = db("user")->select();
		$this->jsonReturnLayUI(0, "", count($user), $user);
	}


	/**
	 *新增用户
	 */
	public function doAddUser()
	{
		$data = input("param.");
		if (empty($data['username'])) {
			$this->jsonReturnLayUI(1, "用户名称不能为空", 0);
		}
		$check = db("user")->where("name", $data['username'])->count();
		if ($check == 0) {
			db("user")->insert(["name" => $data['username'], "price" => 0]);
			$this->jsonReturnLayUI(0, "添加成功", 1, ["name" => $data['username'], "price" => 0]);
		} elseif ($check >= 1) {
			$this->jsonReturnLayUI(1, "用户已存在", 0);
		}
	}

	/**
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * 获取商品信息
	 */
	public function getItemInfo()
	{
		$item = db("item")->select();
		$this->jsonReturnLayUI(0, "", count($item), $item);
	}

	/**
	 *新增商品
	 */
	public function doAddItem()
	{
		$data = input("param.");
		if (empty($data['itemname'])) {
			$this->jsonReturnLayUI(1, "商品名称不能为空", 0);
		}
		if (empty($data['itemtotal'])) {
			$data['itemtotal'] = 0;
		}
		if (empty($data['itemprice'])) {
			$this->jsonReturnLayUI(1, "商品单价不能为空");
		}
		$check = db("item")->where("name", $data['itemname'])->count();
		if ($check == 0) {
			db("item")->insert(["name" => $data['itemname'], "price" => $data['itemprice']]);
			$this->jsonReturnLayUI(0, "添加成功", 1, ["name" => $data['itemname'], "price" => $data['itemprice'], "total" => 0]);
		} elseif ($check >= 1) {
			$this->jsonReturnLayUI(1, "商品已存在", 0);
		}
	}

	/**
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * 获取班组信息
	 */
	public function getGroupInfo()
	{
		$group = db("group")->where("isdelete", 0)->select();
		foreach ($group as $key => $value) {
			$group[$key]['mem_total'] = explode(",", $value['member']);
			$group[$key]['member'] = [];
			if (empty($value['member'])) {
				$group[$key]['member'] = "无";
				$group[$key]['count'] = 0;
			} else {
				$user = db("user")->where("user_id", "in", $value['member'])->select();
				$group[$key]['member'] = implode(", ", array_column((array) $user, 'name'));
				$group[$key]['count'] = count($group[$key]['mem_total']);
			}
			$count = db("count")->where("group_id", $value['group_id'])->select();
			$group[$key]['total'] = 0;
			$group[$key]['price'] = 0;
			foreach ($count as $k => $v) {
				$item = db("item")->where("item_id", $count[$k]['item_id'])->find();
				$group[$key]['total'] += $count[$k]['total'];
				$group[$key]['price'] += $item['price'] * $count[$k]['total'];
			}
		}
		$this->jsonReturnLayUI(0, "", count($group), $group);
	}

	/**
	 *新增班组
	 */
	public function doAddGroup()
	{
		$data = input("param.");
		if (empty($data['groupname'])) {
			$this->jsonReturnLayUI(1, "班组名称不能为空", 0);
		}
		$check = db("group")->where("name", $data['groupname'])->count();
		if ($check == 0) {
			$date = nowtime();
			db("group")->insert(["name" => $data['groupname'], "create_date" => $date]);
			$this->jsonReturnLayUI(0, "添加成功", 1, ["name" => $data['groupname'], "count" => 0, "total" => 0, "price" => 0, "create_date" => $date, "member" => "无"]);
		} elseif ($check >= 1) {
			$this->jsonReturnLayUI(1, "班组已存在", 0);
		}
	}

	/**
	 * @param int $gid 班组ID
	 *
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * 传入班组ID获取班组成员
	 */
	public function getGroupMember($gid = "")
	{
		$res['unselect'] = [];
		$res['select'] = [];
		if ($gid != "") {
			//获取制定班组ID且非软删除的班组信息
			$groupinfo = db("group")->where("group_id", $gid)->where("isdelete", 0)->find();
			//已选择用户ID，转换成int类型
			$group_member = array_map('intval', explode(',', $groupinfo['member']));
			$user = db("user")->select();
			//获取已选择和未选择两个数组的差集
			$res['unselect'] = $this->getDiffArray($user, $group_member);
			//如果已选择用户是0则返回空数组
			if ($group_member[0] == 0) {
				$res['select'] = [];
			} else {
				$res['select'] = $group_member;
			}
			$this->jsonReturnLayUI(0, "", 0, $res);
		} else {
			$this->jsonReturnLayUI(0, "", 0, $res);
		}
	}

	/**
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 *
	 * 新增班组成员
	 */
	public function doAddGroupMember()
	{
		$data = input("param.");
		if (!empty($data['gid'])) {
			//去掉传入参数 [ 和 ] 字符
			db("group")->where("group_id", $data['gid'])->update(["member" => substr($data['member'], 1, strlen($data['member']) - 2)]);
			$this->jsonReturnLayUI(0, "", 0, $data);
		} else {
			$this->jsonReturnLayUI(1);
		}
	}

	/**
	 * @param int $uid 用户ID
	 * @param int $gid 班组ID
	 *
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * 根据传入参数获取个人计件详情或班组计件详情
	 */
	public function getTotal($uid = 0, $gid = 0)
	{
		if ($uid != 0) {
			$count = db("count")->where("user_id", $uid)->select();
			foreach ($count as $key => $value) {
				$name = db("user")->where("user_id", $count[$key]['user_id'])->find();
				$item = db("item")->where("item_id", $count[$key]['item_id'])->find();
				$count[$key]['name'] = $name['name'];
				$count[$key]['item'] = $item['name'];
				$count[$key]['oneprice'] = $item['price'];
				$count[$key]['price'] = $item['price'] * $count[$key]['total'];
			}
			$this->jsonReturnLayUI(0, "", count($count), $count);
		} elseif ($gid != 0) {
			$count = db("count")->where("group_id", $gid)->select();
			foreach ($count as $key => $value) {
				$name = db("group")->where("group_id", $count[$key]['group_id'])->find();
				$item = db("item")->where("item_id", $count[$key]['item_id'])->find();
				$count[$key]['name'] = $name['name'];
				$count[$key]['item'] = $item['name'];
				$count[$key]['oneprice'] = $item['price'];
				$count[$key]['price'] = $item['price'] * $count[$key]['total'];
			}
			$this->jsonReturnLayUI(0, "", count($count), $count);
		} else {
			$this->jsonReturnLayUI(1, "数据获取失败");
		}
	}

}
